package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.mapper.TbItemCatMapper;
import com.usian.mapper.TbItemDescMapper;
import com.usian.mapper.TbItemMapper;
import com.usian.mapper.TbItemParamItemMapper;
import com.usian.pojo.*;
import com.usian.service.ItemService;
import com.usian.utils.IDUtils;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemMapper tbItemMapper;

    @Autowired
    private TbItemDescMapper itemDescMapper;

    @Autowired
    private TbItemParamItemMapper itemParamItemMapper;

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Override
    public TbItem selectItemInfo(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }

    @Override
    public PageResult selectTbItemAllByPage(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        TbItemExample example = new TbItemExample();
        example.setOrderByClause("id desc");
        TbItemExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo((byte) 1);
        List<TbItem> list = tbItemMapper.selectByExample(example);
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        return new PageResult(Long.valueOf(pageInfo.getPages()), pageInfo.getList());
    }

    @Override
    public Integer insertTbItem(TbItem item, String desc, String itemParams) {
        //补齐Tbitem数据
        Long itemId = IDUtils.genItemId();
        Date date = new Date();
        item.setId(itemId);
        item.setStatus((byte) 1);
        item.setUpdated(date);
        item.setCreated(date);
        item.setPrice(item.getPrice() * 100);
        Integer itemNum = tbItemMapper.insertSelective(item);

        //补齐商品描述对象
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        Integer descNum = itemDescMapper.insertSelective(itemDesc);

        //补齐商品规格参数
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setItemId(itemId);
        itemParamItem.setParamData(itemParams);
        itemParamItem.setCreated(date);
        itemParamItem.setUpdated(date);
        Integer itemParamItemNum = itemParamItemMapper.insertSelective(itemParamItem);

        return itemNum + descNum + itemParamItemNum;
    }

    @Override
    public Map<String, Object> preUpdateItem(Long itemId) {
        Map<String, Object> map = new HashMap<>();
        //根据商品id查询商品
        TbItem item = tbItemMapper.selectByPrimaryKey(itemId);
        map.put("item", item);
        //根据商品id查询商品描述
        TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
        map.put("itemDesc", itemDesc);
        //根据商品id查询商品类目
        TbItemCat itemCat = itemCatMapper.selectByPrimaryKey(item.getCid());
        map.put("itemCat", itemCat.getName());
        //根据商品id查询商品规格信息
        TbItemParamItemExample example = new TbItemParamItemExample();
        TbItemParamItemExample.Criteria criteria = example.createCriteria();
        criteria.andItemIdEqualTo(itemId);
        List<TbItemParamItem> list = itemParamItemMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            map.put("itemParamItem", list.get(0).getParamData());
        }
        return map;
    }

    @Override
    public Integer deleteItemById(Long itemId) {
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        tbItem.setStatus((byte) 0);
        Integer itemNum = tbItemMapper.updateByPrimaryKey(tbItem);
        return itemNum;
    }

    @Override
    public Integer updateTbItem(TbItem item, String desc, String itemParams) {
        //补齐Tbitem数据
        Long itemId = IDUtils.genItemId();
        Date date = new Date();
        item.setId(itemId);
        item.setStatus((byte) 1);
        item.setUpdated(date);
        item.setCreated(date);
        item.setPrice(item.getPrice() * 100);
        Integer itemNum = tbItemMapper.updateByPrimaryKeySelective(item);

        //补齐商品描述对象
        TbItemDesc itemDesc = new TbItemDesc();
        itemDesc.setItemId(itemId);
        itemDesc.setItemDesc(desc);
        itemDesc.setCreated(date);
        itemDesc.setUpdated(date);
        Integer descNum = itemDescMapper.updateByPrimaryKeySelective(itemDesc);

        //补齐商品规格参数
        TbItemParamItem itemParamItem = new TbItemParamItem();
        itemParamItem.setItemId(itemId);
        itemParamItem.setParamData(itemParams);
        itemParamItem.setCreated(date);
        itemParamItem.setUpdated(date);
        Integer itemParamItemNum = itemParamItemMapper.updateByPrimaryKeySelective(itemParamItem);

        return itemNum + descNum + itemParamItemNum;
    }
}
